package com.shengyijie.activity.commen;
  

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URL;

import com.shengyijie.activity.MainActivity;
import com.shengyijie.activity.R;      
import com.shengyijie.activity.UserTabActivity;
import com.shengyijie.activity.share.ShareSelectActivity;  
import com.shengyijie.context.ContextApplication; 
import com.shengyijie.util.Utility;

import android.app.Activity;  
import android.content.Intent; 
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;  
import android.widget.Button; 
import android.widget.ImageButton;  
import android.widget.ImageView;
import android.widget.TextView;  
import android.widget.Toast;

public class PushDetailActivity extends Activity implements OnClickListener{
       
	 
	
	 private ImageButton tabHome;
	 private ImageButton tabCategory;
	 private ImageButton tabRecommended;
	 private ImageButton tabUser;
	 private ImageButton tabMore;
	 
	 private TextView tv_name;
	 private TextView tv_type;
	 private TextView tv_define;
	 private TextView tv_des; 
	  
	 
	 private String name; 
	 private String tel; 
	 
	
    public void onCreate(Bundle savedInstanceState){
         super.onCreate(savedInstanceState);
         requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); 
         setContentView(R.layout.pushdetail);
         getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,      // 注意顺序
                 R.layout.title);
         initView(); 
         setItemInfo();
          
    }
     
    public void initView(){
    	TextView title=(TextView) this.findViewById(R.id.title);
		title.setText(ContextApplication.pushMessage.getTitle()); 
		
		Button back=(Button) this.findViewById(R.id.back);
		back.setVisibility(View.VISIBLE);
		back.setOnClickListener(this);
		
		Button share=(Button) this.findViewById(R.id.right);
		share.setVisibility(View.VISIBLE);
		share.setOnClickListener(this);
		 
		im_logo=(ImageView) this.findViewById(R.id.image);
		
		tv_name=(TextView) this.findViewById(R.id.name);
		tv_type=(TextView) this.findViewById(R.id.type);
		tv_define=(TextView) this.findViewById(R.id.define);
		tv_des=(TextView) this.findViewById(R.id.des);
		 
		 
    	tabHome=(ImageButton) this.findViewById(R.id.home);
    	tabCategory=(ImageButton) this.findViewById(R.id.category);
    	tabRecommended=(ImageButton) this.findViewById(R.id.recommended);
    	tabUser=(ImageButton) this.findViewById(R.id.user);
    	tabMore=(ImageButton) this.findViewById(R.id.more);
		try{
		    switch(ContextApplication.prePage){
		       case 0:
		    	   tabHome.setSelected(true);
			       break;
		       case 1:
		    	   tabCategory.setSelected(true);
			       break;
		       case 2:
		    	   tabRecommended.setSelected(true);
			       break;
		       case 3:
		    	   tabUser.setSelected(true);
			       break;
		       case 4 :
		    	   tabMore.setSelected(true);
			       break;
		     }
		 }catch(Exception e){}
		
		 tabHome.setOnClickListener(this);
		 tabCategory.setOnClickListener(this);
		 tabRecommended.setOnClickListener(this);
		 tabUser.setOnClickListener(this);
		 tabMore.setOnClickListener(this);
		  
    }
    
    public void setItemInfo(){
    	try{
    		if(ContextApplication.pushMessage!=null){
    			tv_name.setText(ContextApplication.pushMessage.getTitle());
    			tv_define.setText(ContextApplication.pushMessage.getDes());
    			tv_des.setText(ContextApplication.pushMessage.getDetail());  
    			tv_type.setText(ContextApplication.list[ContextApplication.pushMessage.getType()]);  
    			
    			if(ContextApplication.pushMessage.getImageUrl().equals("")){
    				im_logo.setVisibility(View.GONE);
    			}else{
    				new Task().execute(TASK_IMAGE);
    			}
    			
    			
        	}
    	}catch(Exception e){}
    	
    	
    }
    
    
    
 

	@Override
	public void onClick(View v) { 
		Bundle bd = new Bundle();
        bd.putString("state", "exit");	
        Intent it = new Intent();
        it.putExtras(bd);
		switch(v.getId()){
		     case R.id.home:
			    try{
				   if(ContextApplication.prePage!=0){
					   Intent i=new Intent();
	                	i.setClass(this, MainActivity.class);
	                	i.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
	                	this.startActivity(i);
	                	MainActivity.tabhost.setCurrentTab(0);
	                	ContextApplication.tabtype=0; 
					   this.finish();
				   }
			    }catch(Exception e){}
			    break;
		     case R.id.category:
				    try{
					   if(ContextApplication.prePage!=1){
						   UserTabActivity.tabhost.setCurrentTab(1);
						   setResult(RESULT_OK, it); 
						   this.finish();
					   }
				    }catch(Exception e){}
				    break;
		     case R.id.recommended:
				    try{
					   if(ContextApplication.prePage!=2){
						   UserTabActivity.tabhost.setCurrentTab(2);
						   setResult(RESULT_OK, it); 
						   this.finish();
					   }
				    }catch(Exception e){}
				    break;
		     case R.id.user:
				    try{
					   if(ContextApplication.prePage!=3){
						   UserTabActivity.tabhost.setCurrentTab(3);
						   setResult(RESULT_OK, it); 
						   this.finish();
					   }
				    }catch(Exception e){}
				    break;
		     case R.id.more:
				    try{
					   if(ContextApplication.prePage!=4){
						   UserTabActivity.tabhost.setCurrentTab(4);
						   setResult(RESULT_OK, it); 
						   this.finish();
					   }
				    }catch(Exception e){}
				    break;
		     case R.id.right:
				    try{
				    	ContextApplication.shareName=ContextApplication.pushMessage.getTitle();
				    	ContextApplication.shareUrl=ContextApplication.pushMessage.getPro_url();
				    	if(ContextApplication.shareUrl==null||ContextApplication.shareUrl.equals("")){
				    		ContextApplication.shareUrl="http://www.shengyijie.net";
				    	}
				    	ContextApplication.shareContent="我在生意街商机网发现了“"+ContextApplication.shareName+
								 "”,觉得很不错，分享给大家...\n"+ContextApplication.shareUrl;
				    	Intent i=new Intent();
						i.setClass(this, ShareSelectActivity.class);
						this.startActivity(i);
				    }catch(Exception e){}
				    break;
		     case R.id.back:
				    try{
					  this.finish();
				    }catch(Exception e){}
				    break;
		      
		}
	}
	 
	    
	 private final static String TASK_IMAGE="image"; 
	 private ImageView im_logo;
		
	private class Task extends AsyncTask<String, String, String> {

		@Override
		protected String doInBackground(String... params) {
			  if(params[0].equals(TASK_IMAGE)){
				return getShopImage();
			}else
				return ""; 
		} 
		@Override
		protected void onPostExecute(String result) {				
			super.onPostExecute(result);
            if(result.equals("image_ok")){
				try{
					if(logo!=null)
						im_logo.setBackgroundDrawable(logo);
				}catch(Exception e){}
			}
		} 
		@Override
		protected void onPreExecute() { 
			super.onPreExecute();  
		} 
		@Override
		protected void onProgressUpdate(String... values) {
			super.onProgressUpdate(values);
		}
	}
	Drawable logo=null;
	 public String getShopImage(){
		 String result="image_fail";
		 logo=null;
		 try{
			   String fileName=Utility.getPictureFileName(ContextApplication.pushMessage.getImageUrl()); 
				
				URL m;
				InputStream i = null; 
				try {
					if(!fileName.equals("")){
					   File f = new File(Environment.getExternalStorageDirectory(),"shengyijie/"+fileName);
					   FileInputStream is = null;
					   is = new FileInputStream(f);
					   Bitmap bm;
					   bm = BitmapFactory.decodeStream(is, null, null);
					   logo = new BitmapDrawable(bm);
					}
				}catch (Exception e) {}
				
				if(logo == null){
					try {
						m = new URL(ContextApplication.project.getPro_pic_show());
						i = (InputStream) m.getContent();
						logo = Drawable.createFromStream(i, "src");
						i.close();
						m=null;
						i=null;
						if(logo!=null){
							result="image_ok";
							if( !fileName.equals("")){ 
							   Utility.saveImage(fileName,logo);
							}
						}
						
					}catch (Exception e) {}
				} else{
					result="image_ok";
				}
			 
		}catch(Exception e){}
		return result;
	 }
	 
    
}